//
//  CJTopLineSubController.m
//  Boss
//
//  Created by admin on 2018/9/12.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "CJTopLineSubController.h"
#import "CJTopLineDetailController.h"
#import "CJTopLineOneCell.h"
#import "CJTopLineThreeCell.h"
#import "CJTopLineBigCell.h"



@interface CJTopLineSubController ()
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation CJTopLineSubController
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self loadData];
    // Do any additional setup after loading the view.
}

-(void)setupUI
{
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"CJTopLineOneCell" bundle:nil] forCellReuseIdentifier:@"CJTopLineOneCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"CJTopLineThreeCell" bundle:nil] forCellReuseIdentifier:@"CJTopLineThreeCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"CJTopLineBigCell" bundle:nil] forCellReuseIdentifier:@"CJTopLineBigCell"];
    self.tableView.estimatedRowHeight = 100;
    
}
-(void)loadData
{
    WEAKSELF
    NSString *sortId = [NSString stringWithFormat:@"%ld",self.sortId];
    
    [WYHttpServer getMerchantTopLineWithSortId:sortId WithCurrentPage:@"1" WithPageSize:@"20" SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
                [weakSelf.dataArray addObjectsFromArray:[CJBOSSHeadLine getHeadLineFromJson:response[@"result"][@"detail"]]];
                [weakSelf.tableView reloadData];
        }
        
        
        
        
    }];
    
    
    
    
    
    
    
    
//    [WYHttpServer getMerchantBrandWithSortId:sortId WithCurrentPage:@"1" WithPageSize:@"20" SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
//        if (success) {
//            [weakSelf.dataArray addObjectsFromArray:[CJBOSSRecommend getRemmendModelFromJson:response[@"result"][@"detail"]]];
//            [weakSelf.tableView reloadData];
//        }
//
//
//    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CJBOSSHeadLine *bossLine = self.dataArray[indexPath.row];
    if (bossLine.headlineType.intValue==1) {
        CJTopLineOneCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CJTopLineOneCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"CJTopLineOneCell" owner:nil options:nil].firstObject;
        }
        [cell setBossLine:bossLine];
        //  cell.backgroundColor = [UIColor yellowColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (bossLine.headlineType.intValue==2) {
        CJTopLineThreeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CJTopLineThreeCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"CJTopLineThreeCell" owner:nil options:nil].firstObject;
        }
        [cell setBossLine:bossLine];
        //  cell.backgroundColor = [UIColor redColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (bossLine.headlineType.intValue==3) {
        CJTopLineBigCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CJTopLineBigCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"CJTopLineBigCell" owner:nil options:nil].firstObject;
        }
        [cell setBossLine:bossLine];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //  cell.backgroundColor = [UIColor purpleColor];
        return cell;
    }
    return nil;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CJTopLineDetailController *tdc = [[CJTopLineDetailController alloc]init];
    CJBOSSHeadLine *bossLine = self.dataArray[indexPath.row];
    tdc.navTitle = bossLine.firmSimpleName;
    tdc.topLineID = bossLine.headlineId;
    
    [self.navigationController pushViewController:tdc animated:YES];
}



- (UITableView *)tableView
{
    if (_tableView == nil) {
        CGRect frame = CGRectMake(0, 0, self.view.width, kScreenHeight-self.navHeight-44);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    }
    return _tableView;
}


@end
